package org.jsola.hr.web;

import org.jsola.core.Result;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendPunchDTO;
import org.jsola.hr.query.AttendPunchCheckQuery;
import org.jsola.hr.service.IAttendPunchService;
import org.jsola.hr.vo.AttendPunchCheckSimpleVO;
import org.jsola.hr.vo.AttendPunchCheckVO;
import org.jsola.hr.vo.AttendPunchVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * @author wxz
 */
@RestController("hrAttendPunchController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendPunch")
public class AttendPunchController implements IAttendPunchControllerAPI {

	@Autowired
	private IAttendPunchService attendPunchService;
	

    @Override
    @PostMapping(value = "checkPunch")
    public Result<AttendPunchCheckSimpleVO> checkPunch(@RequestBody AttendPunchCheckQuery attendPunchCheckQuery) {
    	
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤方案权限校验

        AttendPunchCheckVO attendPunchCheckVO = attendPunchService.checkPunch(attendPunchCheckQuery, tokenUser);

        AttendPunchCheckSimpleVO attendPunchCheckSimpleVO = attendPunchCheckVO.to(AttendPunchCheckSimpleVO.class);

        return Result.success(
                attendPunchCheckSimpleVO
        );
    }
    
    /**
     * 
     */
    @Override
    @PostMapping(value = "punch")
    public Result<AttendPunchVO> punch(@RequestBody AttendPunchDTO attendPunchDTO) {
    	
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤方案权限校验

        return Result.success(
        		attendPunchService.punch(attendPunchDTO, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "punchBatchSimulation")
    public Result<AttendPunchVO> punchBatchSimulation(@RequestBody AttendPunchDTO attendPunchDTO) {
        try {
            attendPunchService.punchBatchSimulation(attendPunchDTO);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return Result.success(null);
    }
}
